#!/usr/bin/env python
# @Software: PyCharm
# @Time : 2022/9/16 16:46
# @Author : v_nahuachen
# @Version：V 0.1
# @File : native_test.py
# @desc :

import minium
from cases.base.base_case import BaseCase
from cases.pages import home_page
from cases.pages import base_page


@minium.ddt_class()
class NativeTest(BaseCase):

    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        try:
            # 清空授权
            cls.mini.clear_auth()
        except:
            pass

    def setUp(self) -> None:
        super().setUp()
        # 页面跳转
        self.HomePage.interface_page("native")

    def __init__(self, methodName='runTest'):
        """
        初始化Native实例类
        """
        super(NativeTest, self).__init__(methodName)
        self.HomePage = home_page.HomePage(self)
        self.BasePage = base_page.BasePage(self)

    def test_01_show_modal(self):
        """
        showModal弹框授权
        :return:
        """
        is_called, callback_args = self.BasePage.hook_native_method(
            method="showModal",
            selector="#testModal",
            attr_method="handle_modal")

        self.assertTrue(is_called, "callback called")
        # 小程序API showModal方法回调信息断言
        self.assertDictContainsSubset(
            {"errMsg": "showModal:ok"}, callback_args
        )

    def test_02_mock_show_modal(self):
        """
        showModal弹窗 mock示例
        :return:
        """
        expect_result = {"errMsg": "showModal:ok", "cancel": "false",
                         "confirm": "true", "content": "modal content"}

        info = self.BasePage.mock_wx_method("showModal", expect_result)
        self.assertDictContainsSubset(
            {"errMsg": "showModal:ok"}, info, "mock 模态弹窗"
        )
    #
    def test_11_get_location(self):
        """
        地理位置授权弹框
        :return:
        """
        is_called, callback_args = self.BasePage.hook_native_method(
            method="getLocation",
            selector="#testGetLocation",
            attr_method="allow_get_location")

        self.logger.warn(callback_args)
        self.assertTrue(is_called, "callback called")
        self.assertDictContainsSubset(
            {"errMsg": "getLocation:ok"}, callback_args, "获取位置")

    def test_12_mock_get_location(self):
        """
        地理位置授权弹窗 mock示例
        :return:
        """
        expect_result = {'speed': -1, 'steps': 0, 'errMsg': 'getLocation:ok',
                         'longitude': 113.3249, 'latitude': 23.0999, 'accuracy': 65}

        info = self.BasePage.mock_wx_method("getLocation", expect_result)
        self.assertDictContainsSubset(
            info, expect_result, "mock 获取位置"
        )

    def test_21_get_user_profile(self):
        """
        用户信息授权弹框
        :return:
        """
        is_called, callback_args = self.BasePage.hook_native_method(
            method="getUserProfile",
            selector="#getUserProfile",
            attr_method="allow_get_user_info")
        self.assertTrue(is_called, "callback called")
        self.assertDictContainsSubset(
            {"errMsg": "getUserProfile:ok"}, callback_args, "授权用户信息")

    def test_22_mock_get_user_profile(self):
        """
        mock 用户信息弹窗
        :return:
        """
        expect_result = {"errMsg": "getUserProfile:ok"}
        info = self.BasePage.mock_wx_method("getUserProfile", expect_result)
        self.assertDictContainsSubset(
            {"errMsg": "getUserProfile:ok"}, info, "mock 用户信息弹窗")

    def test_31_request_subscribe_message(self):
        """
        订阅信息授权弹框
        :return:
        """
        is_called, callback_args = self.BasePage.hook_native_method(
            method="requestSubscribeMessage",
            selector="#requestSubscribeMessage",
            attr_method="allow_send_subscribe_message")

        self.assertTrue(is_called, "callback called")
        self.assertDictContainsSubset(
            {"errMsg": "requestSubscribeMessage:ok"}, callback_args, "订阅消息")

    def test_32_mock_request_subscribe_message(self):
        """
        mock 订阅消息
        :return:
        """
        expect_result = {"errMsg": "requestSubscribeMessage:ok"}
        info = self.BasePage.mock_wx_method("requestSubscribeMessage", expect_result)
        self.assertDictContainsSubset(
            {"errMsg": "requestSubscribeMessage:ok"}, info, "mock 订阅消息")

    def test_41_map_select_location(self):
        """
        地图选择
        :return:
        """
        expected_res = {
            "errMsg": "chooseLocation:ok",
            "name": "腾讯微信总部",
            "address": "广东省广州市海珠区tit创意园品牌街",
            "latitude": 23.100039,
            "longitude": 113.32456,
        }

        is_called, callback_args = self.BasePage.hook_native_method(
            method="chooseLocation",
            selector="#testChooseLocation",
            attr_method="map_select_location",
            value="腾讯微信总部")
        self.assertTrue(is_called, "callback called")
        self.assertDictContainsSubset(expected_res, callback_args)

    def test_42_map_back_choose_location(self):
        """
        地图取消选择，并返回
        :return:
        """
        is_called, callback_args = self.BasePage.hook_native_method(
            method="chooseLocation",
            selector="#testChooseLocation",
            attr_method="map_back_to_mp")

        self.assertTrue(is_called, "callback called")
        self.assertDictContainsSubset({"errMsg": "chooseLocation:fail cancel"}, callback_args)

    @minium.ddt_case(
        ({
            "errMsg": "chooseLocation:ok",
            "name": "腾讯微信总部",
            "address": "广东省广州市海珠区tit创意园品牌街",
            "latitude": 23.100039,
            "longitude": 113.32456,
        }),
        ({"errMsg": "chooseLocation:fail cancel"}),
    )
    def test_43_mock_choose_location(self, args):
        """
        mock 地图选择
        :param args:
        :return:
        """
        expect_result = args

        try:
            # mock chooseLocation方法，获取回调
            info = self.BasePage.mock_wx_method("chooseLocation", expect_result)
        except minium.MiniAppError as e:
            self.logger.warn(e)
            self.assertDictContainsSubset(
                {"errMsg": "chooseLocation:fail cancel"}, expect_result, "mock 选择位置"
            )
            return
        self.assertDictContainsSubset(
            info, expect_result, "mock 选择位置"
        )

    @minium.ddt_case(
        ("A", {"errMsg": "showActionSheet:ok", "tapIndex": 0}),
        ("测试", {"errMsg": "showActionSheet:ok", "tapIndex": 1}),
        ("C", {"errMsg": "showActionSheet:ok", "tapIndex": 2}),
        ("取消", {"errMsg": "showActionSheet:fail cancel"}),
    )
    def test_51_handle_action_sheet(self, args):
        """
        下拉菜单
        :param args:
        :return:
        """
        [item, cb_args] = args
        is_called, callback_args = self.BasePage.hook_native_method(
            method="showActionSheet",
            selector="#testActionSheet",
            attr_method="handle_action_sheet",
            value=item)

        self.assertTrue(is_called, "callback called")
        self.assertDictEqual(cb_args, callback_args, "上拉菜单")

    @minium.ddt_case(
        ("A", {"errMsg": "showActionSheet:ok", "tapIndex": 0}),
        ("测试", {"errMsg": "showActionSheet:ok", "tapIndex": 1}),
        ("C", {"errMsg": "showActionSheet:ok", "tapIndex": 2}),
        ("取消", {"errMsg": "showActionSheet:fail cancel"}),
    )
    def test_52_mock_action_sheet(self, args):
        """
        mock 下拉菜单
        :param args:
        :return:
        """
        [item, expect_result] = args

        try:
            info = self.BasePage.mock_wx_method("showActionSheet", expect_result)
        except minium.MiniAppError as e:
            self.logger.warn(e)
            self.assertEqual(
                "cancel", "cancel", "mock 显示菜单"
            )
            return

        self.assertDictContainsSubset(
            {"errMsg": "showActionSheet:ok"}, info, "mock 显示菜单"
        )

    def test_forward_miniprogram(self):
        """
        把当前小程序页面分享给 "xxx"，内容text 为 "hello"
        :return:
        """
        self.native.forward_miniprogram("aaui", "hello", False)

    def test_forward_miniprogram_inside(self):
        """
        处理由分享按钮弹出的分享窗口
        把当前小程序页面分享给 "xxx"，内容text 为 "hello"
        :return:
        """
        self.BasePage.get_elements_selector("#testshare").tap()
        self.page.wait_for(2)
        self.native.forward_miniprogram_inside("aaui", "hello", False)
